//
//  DetailModel.m
//  MusicNews
//
//  Created by lanou3g on 15/10/15.
//  Copyright © 2015年 LC.J. All rights reserved.
//

#import "DetailModel.h"
#import "DetailImageModel.h"
@implementation DetailModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"body"]) {
        self.bodyString = value;
        
        /**
         *  在for循环上对每个<!--IMG#%d-->都进行替换
         */
        for (int i = 0; i < self.img.count; i++) {
            NSString *src = [[self.img objectAtIndex:i] objectForKey:@"src"];
            /**
             *  "pixel": "700*946",---规格
             
             */
            NSString *pixel = [[self.img objectAtIndex:i] objectForKey:@"pixel"];
            NSString *subPixel = [pixel substringFromIndex:4];
            
            /**
             *  进行字符替换
             */
//            NSString *body1 = [self.bodyString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--IMG#%d-->", i] withString:[NSString stringWithFormat:@"<img src = \"%@\" width=\"%f\" height=\"%@\">", src, ([UIApplication sharedApplication].keyWindow.frame.size.width - 20), subPixel]];
//            
//            self.bodyString = (NSMutableString *) body1;
            
        }
    }
    
}

+ (instancetype)detailWithDict:(NSDictionary *)dict
{
    DetailModel *detailModel = [[self alloc] init];
    detailModel.title = dict[@"title"];
    detailModel.ptime = dict[@"ptime"];
    detailModel.bodyString = dict[@"body"];
    NSArray *imgArray = dict[@"img"];
    NSMutableArray *temArray = [NSMutableArray arrayWithCapacity:imgArray.count];
    for (NSDictionary *dict1 in imgArray) {
        DetailImageModel *imageModel = [DetailImageModel detailImgWithDict:dict1];
        [temArray addObject:imageModel];
    }
    detailModel.img = temArray;
    return detailModel;
}


@end
